﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using FreedomERP.Models;
using FreedomERP.Comm;
using FreedomERP.Services.Bd;
using FreedomERP.Services.Bd.Impl;
using System.Collections;
using Ext.Net.MVC;
using FreedomERP.Comm.Utils;
using FreedomERP.Models.Bd;
using Ext.Net;
using FreedomERP.Filter;

namespace FreedomERP.Controllers
{
    [CheckSessionFilterAttribute]
    public class ProductController : Controller
    {

        IBdProductService productService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (productService == null) { productService = new BdProductServiceImpl(); }

            base.Initialize(requestContext);
        }

        //
        // GET: /Product/

        public ActionResult ProductIndex()
        {
            return View();
        }

        public AjaxStoreResult ProductListPaging(String fNumber, String fName, String fModelId, String fReMark, String fProdInvStatusName, String sort, String dir, int start, int limit)
        {
            int totalCount = 0;
            Boolean isASC = true;
            if ("DESC" == dir)
            {
                isASC = false;
            }
            //List<LCProductInfo> products = productService.getProductInfos(null, fNumber, fName, fModelId, null, fReMark, fProdInvStatusName, sort, isASC, start, limit, out totalCount);
            string currAccountOrgunitId = BaseController.GetCurrLoginAccountOrgunitFId();
            List<LCProductInfo> products = productService.getProductInfos(null, fNumber, fName, fModelId, null, fReMark, fProdInvStatusName, sort, isASC, start, limit, out totalCount, currAccountOrgunitId);
            AjaxStoreResult productsPaging = new AjaxStoreResult(products, totalCount);
            return productsPaging;
        }


        public AjaxStoreResult getProductListByID(String fId)
        {
            int totalCount = 0;
            string currAccountOrgunitId = BaseController.GetCurrLoginAccountOrgunitFId();
            List<LCProductInfo> products = productService.getProductInfos(fId, null, null, null, null, null, null, null, true, 0, 1, out 	totalCount, currAccountOrgunitId);

            AjaxStoreResult productsPaging = new AjaxStoreResult(products, products.Count);
            return productsPaging;
        }



        public AjaxStoreResult GetModelTypes()
        {
            string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();
            List<ProductModelType> modelTypes = productService.getModelTypeList(currAccountOrgunitFId);
            AjaxStoreResult modelTypesResult = new AjaxStoreResult(modelTypes, modelTypes.Count);
            return modelTypesResult;
        }


        public AjaxFormResult SaveOrUpadteProduct(String fId, String fNumber, String fName, String fModelId, String fUnitId, String fReMark, String fQty, String fPrice1, String fPrice2, String fPrice3)
        {
            String resultFID = null;
            AjaxFormResult response = new AjaxFormResult();
            string ResultMsg = string.Empty;
            bool ResultBool = true;
            try
            {
                LCProductInfo productInfo = new LCProductInfo();
                if (fId == null || fId.Equals(ConstData.EMPTY_STRING))
                {

                }
                else
                {
                    productInfo.ProductId = fId;
                }
                String orgunitId = BaseController.GetCurrLoginAccountOrgunitFId();
                IBdOrgunitService orgunitService = new BdOrgunitServiceImpl();
                //orgunitId = orgunitService.findOrgunitId(ConstData.ORGUNIT_NAME_LC);
                productInfo.ProductModelId = fModelId;
                productInfo.ProductUnitId = fUnitId;
                productInfo.ProductName = fName;
                productInfo.ProductNumber = fNumber;
                productInfo.ProductOrgId = orgunitId;
                productInfo.ProductQty = Convert.ToInt32(fQty);
                productInfo.ProductRemark = fReMark;
                productInfo.ProductPrice1 = fPrice1;
                productInfo.ProductPrice2 = fPrice2;
                productInfo.ProductPrice3 = fPrice3;
                productService.saveOrUpdateProductInfo(productInfo, BaseController.GetCurrLoginAccountFId(), out resultFID);
            }
            catch (System.Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
                return response;
            }


            response.Success = ResultBool;
            response.ExtraParams["msg"] = resultFID;
            return response;
        }


        public ActionResult DeleteProduct(String fId)
        {
            AjaxResult response = new AjaxResult();
            string ResultMsg = string.Empty;
            try
            {
                productService.deleteProduct(fId);
            }
            catch (System.Exception ex)
            {

                response.ErrorMessage = ex.Message;

            }
            return response;
        }

        /// <summary>
        /// 获取产品信息
        /// 作者：梁建东
        /// </summary>
        public AjaxResult GetProductMsgByFNumber(string FNumber, string priceType)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string currAccountOrgunitId = BaseController.GetCurrLoginAccountOrgunitFId();
                int totalCount = 0;
                List<LCProductInfo> products = productService.getProductInfos(null, FNumber, null, null, null, null, null, null, true, 0, 1, out 	totalCount, currAccountOrgunitId);

                if (products != null && products.Count > 0)
                {
                    LCProductInfo product = products.First<LCProductInfo>();

                    Parameter pFId = new Parameter();
                    pFId.Name = "FId";
                    pFId.Value = product.ProductId;
                    response.ExtraParamsResponse.Add(pFId);

                    Parameter pFName = new Parameter();
                    pFName.Name = "FName";
                    pFName.Value = product.ProductName;
                    response.ExtraParamsResponse.Add(pFName);

                    Parameter pFProductModelName = new Parameter();
                    pFProductModelName.Name = "FProductModelName";
                    pFProductModelName.Value = product.ProductModelName;
                    response.ExtraParamsResponse.Add(pFProductModelName);

                    Parameter pFInventoryQty = new Parameter();
                    pFInventoryQty.Name = "FProductInventoryQty";
                    pFInventoryQty.Value = product.ProductQty + "";
                    response.ExtraParamsResponse.Add(pFInventoryQty);

                    Parameter pFPrice = new Parameter();
                    pFPrice.Name = "FPrice";
                    if (priceType.Equals(ConstData.CATEGORY_PROD_PRICE_LC_TYPE1))
                    {
                        pFPrice.Value = product.ProductPrice1;
                    }
                    else if (priceType.Equals(ConstData.CATEGORY_PROD_PRICE_LC_TYPE2))
                    {
                        pFPrice.Value = product.ProductPrice2;
                    }
                    else if (priceType.Equals(ConstData.CATEGORY_PROD_PRICE_LC_TYPE3))
                    {
                        pFPrice.Value = product.ProductPrice3;
                    }
                    response.ExtraParamsResponse.Add(pFPrice);

                    Parameter pResult = new Parameter();
                    pResult.Name = "Result";
                    pResult.Value = "true";
                    response.ExtraParamsResponse.Add(pResult);
                }
                else
                {
                    Parameter pResult = new Parameter();
                    pResult.Name = "Result";
                    pResult.Value = "false";
                    response.ExtraParamsResponse.Add(pResult);
                }
            }
            catch (Exception e)
            {
                response.Result = false;
                response.ErrorMessage = e.Message;
                return response;
            }
            return response;
        }

        /// <summary>
        /// 获取产品信息
        /// <author>梁建东</author>
        /// </summary>
        public AjaxResult GetProductMsgByFNumberGeLi(string FNumber)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string currAccountOrgunitId = BaseController.GetCurrLoginAccountOrgunitFId();
                int totalCount = 0;
                List<LCProductInfo> products = productService.getProductInfos(null, FNumber, null, null, null, null, null, null, true, 0, 1, out 	totalCount, currAccountOrgunitId);
                if (products != null && products.Count > 0)
                {
                    LCProductInfo product = products.First<LCProductInfo>();

                    Parameter pFId = new Parameter();
                    pFId.Name = "FId";
                    pFId.Value = product.ProductId;
                    response.ExtraParamsResponse.Add(pFId);

                    Parameter pFNumber = new Parameter();
                    pFNumber.Name = "FNumber";
                    pFNumber.Value = product.ProductNumber;
                    response.ExtraParamsResponse.Add(pFNumber);

                    Parameter pFName = new Parameter();
                    pFName.Name = "FName";
                    pFName.Value = product.ProductName;
                    response.ExtraParamsResponse.Add(pFName);

                    Parameter pFProductModelName = new Parameter();
                    pFProductModelName.Name = "FProductModelName";
                    pFProductModelName.Value = product.ProductModelName;
                    response.ExtraParamsResponse.Add(pFProductModelName);

                    Parameter pFInventoryQty = new Parameter();
                    pFInventoryQty.Name = "FProductInventoryQty";
                    pFInventoryQty.Value = product.ProductQty + "";
                    response.ExtraParamsResponse.Add(pFInventoryQty);

                    Parameter pFProductUnitName = new Parameter();
                    pFProductUnitName.Name = "FProductUnitName";
                    pFProductUnitName.Value = product.ProductUnitName;
                    response.ExtraParamsResponse.Add(pFProductUnitName);

                    Parameter pResult = new Parameter();
                    pResult.Name = "Result";
                    pResult.Value = "true";
                    response.ExtraParamsResponse.Add(pResult);
                }
                else
                {
                    Parameter pResult = new Parameter();
                    pResult.Name = "Result";
                    pResult.Value = "false";
                    response.ExtraParamsResponse.Add(pResult);
                }
            }
            catch (Exception e)
            {
                response.Result = false;
                response.ErrorMessage = e.Message;
                return response;
            }
            return response;
        }

        /// <summary>
        /// 获取产品单位列表
        /// <author>梁建东</author>
        /// </summary>
        public AjaxStoreResult GetUnits()
        {
            string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();
            List<ProductUtil> modelTypes = productService.getUnitList(currAccountOrgunitFId);
            AjaxStoreResult modelTypesResult = new AjaxStoreResult(modelTypes, modelTypes.Count);
            return modelTypesResult;
        }

        /// <summary>
        /// 产品进出货统计页面
        /// <author>梁建东</author>
        /// </summary>
        public ActionResult ProductOutInCount()
        {
            return View();
        }

        /// <summary>
        /// 产品进出货统计
        /// <author>梁建东</author>
        /// </summary>
        public AjaxStoreResult ProductOutInCountList(string FProductNumber, string FProductName, string FDateFrom, string FDateTo)
        {
            Hashtable filters = new Hashtable();
            filters.Add("FProductNumber", FProductNumber);
            filters.Add("FProductName", FProductName);
            filters.Add("FDateFrom", FDateFrom);
            filters.Add("FDateTo", FDateTo);
            filters.Add("orgunitParam", BaseController.GetCurrLoginAccountOrgunitFId());

            List<ProductOutInCount> list = productService.ProductOutInCount(filters);

            AjaxStoreResult result = new AjaxStoreResult(list, list.Count);

            return result;
        }

        /// <summary>
        /// 导出送货单信息到excel
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public ActionResult ExportProductOutInCountToExcel(string FProductNumber, string FProductName, string FDateFrom, string FDateTo)
        {
            AjaxResult response = null;
            string ResultMsg = string.Empty;
            try
            {
                string serverPath = Server.HtmlEncode(Request.PhysicalApplicationPath);

                Hashtable filters = new Hashtable();
                filters.Add("FProductNumber", FProductNumber);
                filters.Add("FProductName", FProductName);
                filters.Add("FDateFrom", FDateFrom);
                filters.Add("FDateTo", FDateTo);
                filters.Add("orgunitParam", BaseController.GetCurrLoginAccountOrgunitFId());

                productService.ExportProductOutInCountToExcel(filters, serverPath, Response);
            }
            catch (System.Exception ex)
            {
                response = new AjaxResult();
                response.ErrorMessage = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取产品库存状态类型
        /// </summary>
        /// <author>陈新杰</author>
        public AjaxStoreResult getProdInvStatusTypeList()
        {
            List<ProductInvStatusType> modelTypes = productService.getProdInvStatusTypeList();

            AjaxStoreResult modelTypesResult = new AjaxStoreResult(modelTypes, modelTypes.Count);
            return modelTypesResult;
        }

        /// <summary>
        /// 导出送货单信息到excel
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public ActionResult ExportProductInfosToExcel(String fNumber, String fName, String fModelId, String fReMark, String fProdInvStatusName, String sort, String dir)
        {
            AjaxResult response = null;
            string ResultMsg = string.Empty;
            try
            {
                string serverPath = Server.HtmlEncode(Request.PhysicalApplicationPath);
                productService.ExportProductsInfosToExcel(serverPath, Response, fNumber, fName, fModelId, fReMark, fProdInvStatusName, sort, dir, BaseController.GetCurrLoginAccountOrgunitFId(), BaseController.GetCurrLoginAccountOrgunitFName());
            }
            catch (System.Exception ex)
            {
                response = new AjaxResult();
                response.ErrorMessage = ex.Message;
            }
            return response;
        }

        public ActionResult ProductIndexGeLi()
        {
            return View();
        }

    }


     
}
